﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpSvn;
using System.Net;
using System.Collections.ObjectModel;
using System.IO;

namespace monitorapp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Leeremos propiedades del SVN");
            Console.WriteLine("https://monitor-app.googlecode.com/svn/trunk");
            //iniciando proceso
            
            SvnClient client = new SvnClient();
            client.Authentication.DefaultCredentials = new NetworkCredential("victorvsnet@gmail.com", "qP9Wy8nx3Jp7");//opcional
            
            //otro
            // get logitems
            Collection<SvnLogEventArgs> logitems;
            client.GetLog(new Uri("https://monitor-app.googlecode.com/svn/trunk"), out logitems);

            var result = new List<SvnFile>();

            // get craation date for each
            foreach (var logitem in logitems.OrderBy(logitem => logitem.Time))
            {
                foreach (var changed_path in logitem.ChangedPaths)
                {
                    string filename = Path.GetFileName(changed_path.Path);
                    if (changed_path.Action == SvnChangeAction.Add)
                    {
                        result.Add(new SvnFile() { Name = filename, Created = logitem.Time });
                        Console.WriteLine(filename + " # " + logitem.Time.ToString());
                    }
                }
            }

            Console.ReadLine();
            //otro

            using (SvnClient svnClient = new SvnClient())
            {
                Collection<SvnListEventArgs> contents;
                List<string> files = new List<string>();
                if (svnClient.GetList(new Uri("https://monitor-app.googlecode.com/svn/trunk"), out contents))
                {
                    foreach (SvnListEventArgs item in contents)
                    {
                        string thour;
                        thour = item.Entry.Time.Hour.ToString();
                        Console.WriteLine(item.Path + " # " + item.Entry.FileSize + " # " + item.Entry.Time.ToString());
                    }
                }
            }

            //finalizando proceso
            Console.WriteLine();
            Console.WriteLine("Presione enter para finalizar");
            Console.ReadLine();
        }

        private List<SvnFile> GetSvnFiles(SvnClient client, String uri_path)
        {
            // get logitems
            Collection<SvnLogEventArgs> logitems;
            client.GetLog(new Uri(uri_path), out logitems);

            var result = new List<SvnFile>();

            // get craation date for each
            foreach (var logitem in logitems.OrderBy(logitem => logitem.Time))
            {
                foreach (var changed_path in logitem.ChangedPaths)
                {
                    string filename = Path.GetFileName(changed_path.Path);
                    if (changed_path.Action == SvnChangeAction.Add)
                    {
                        result.Add(new SvnFile() { Name = filename, Created = logitem.Time });
                    }
                }
            }

            return result;
        }

        private List<String> getFolderFiles(SvnClient client, SvnTarget folderTarget)
        {
            bool gotList;
            List<String> files = new List<String>();

            using (SvnClient _client = new SvnClient())
            {
                Collection<SvnListEventArgs> list;

                gotList = _client.GetList(folderTarget, out list);

                if (gotList)
                {
                    foreach (SvnListEventArgs item in list)
                    {
                        files.Add(item.Path);
                    }
                }
            }
            return files;
        }

    }
}
